#!/bin/csh -f
#
# $Id: lasergnu,v 1.1 1999/03/26 21:48:14 lhecking Exp $
#
# Print gnuplot output on an Imagen or Postscript laser printer.

set print_banner = on   # Print a banner page unless told otherwise.
set input_files = ()    # the plot input command files
set lpr_opts = ()       # options to lpr

# Default printer set by shell variable PRINTER.
if (! $?PRINTER) then
    if ($?LASER) then
        set PRINTER=$LASER
    else
        set PRINTER="lw0"
    endif
endif
set printer = (-P$PRINTER)

# File for plot commands, and for plot output
set TMP=/tmp/plot$$
set outfile=$TMP.out    # the output file
onintr cleanup

# default is Imagen mode for Imagen printer; see -p option
set setterm="set terminal imagen"
set LANG="-Limpress"

# Loop through the command-line arguments.

top:
    if ($#argv > 0) then

        switch ("$argv[1]")

        case -b*:   # Do not print a banner page.
        case -J*:   # Compatible with imprint.
            set print_banner = off
                set lpr_opts=($lpr_opts -h)
            shift argv
            goto top

        case -f?*:  # Specify file containing plot commands
            set input_files = ($input_files `echo $argv[1] | sed 's/^-f//'`)
            shift argv
            goto top

        case -f:    # Specify file containing plot commands
            shift argv
            if ($#argv > 0) then
                set input_files = ($input_files $argv[1])
                shift argv
            else
                echo "Usage: -f file ..."
                echo "Type    lasergnu -help    for help."
                exit (1)
            endif
            goto top

              case -F?*:              # Specify font 
                      set fontsize = `echo $argv[1] | sed 's/^-F//'`)
                      shift argv
                      goto top

              case -F:        # Specify font 
                      shift argv
                      if ($#argv > 0) then
                              set font = $argv[1]
                              shift argv
                      else
                              echo "Usage: -F font"
                              echo "Type    lasergnu -help    for help."
                              exit (1)
                      endif
                      goto top

        case -t?*:  # Specify title of plot
            echo set title \""`echo $argv[1] | sed 's/^-t//'`"\" >> $TMP
            shift argv
            goto top

        case -t:    # Specify title of plot
            shift argv
            if ($#argv > 0) then
                echo set title \""$1"\" >> $TMP
                shift argv
            else
                echo "Usage: -t title ..."
                echo "Type    lasergnu -help    for help."
                exit (1)
            endif
            goto top
        case -help:
			goto usage

        case -P?*:  # Set the printer, exactly as by itroff.
            set printer = $argv[1]
            shift argv
            goto top

        case -P:    # Set the printer, exactly as by itroff.
            shift argv
            if ($#argv > 0) then
                set printer = (-P$argv[1])
                shift argv
            else
                echo "Usage: -P printer ..."
                echo "Type    lasergnu -help    for help."
                exit (1)
            endif
            goto top

                # use impress
        case -I:
             echo Imagen is the default mode now
             shift argv
             goto top

                # use postscript instead of impress language
        case -p:
             set setterm="set term postscript"
             set LANG="-Lpostscript"
             shift argv
             goto top

		case -s?*:	# Specify scale of plot
			echo set size `echo $argv[1] | sed 's/^-s//'` >> $TMP
			shift argv
			goto top

		case -s:	# Specify scale of plot
			shift argv
			if ($#argv > 0) then
				echo set size "$1" >> $TMP
				shift argv
			else
				echo "Usage: -s xscale,yscale"
				echo "Type    lasergnu -help    for help."
				exit (1)
			endif
			goto top

		case -w?*:	# Specify split page in multiplot (horiz,vertical)
			set windows=`echo "$argv[1]\]" | sed 's/^-s/[/'`
			shift argv
			goto top

		case -w:	# Specify split page
			shift argv
			if ($#argv > 0) then
				set windows="[$argv[1]]"
				shift argv
			else
				echo "Usage: -w horiz,vertical"
				echo "Type    lasergnu -help    for help."
				exit (1)
			endif
			goto top

    	   	case -O:
	   		 set orientation="portrait"
			 shift argv
	   		 goto top

        case -?*:
            echo "I do not recognize option $argv[1]."
			goto usage

        default:
              echo "$argv[1]"   >> $TMP
            shift argv
            goto top

        endsw
    endif

# try to devine the printer type
if ($printer =~ -Plw*) then
    set setterm="set term postscript"
    set LANG="-Lpostscript"
endif

if ($printer =~ -Pim*) then
    set setterm="set term imagen $windows $orientation $font"
    set LANG="-Limpress"
endif

# Set up input file
echo "$setterm" > $TMP.plt
echo set output \"$outfile\" >> $TMP.plt
if (-e $TMP) cat $TMP >> $TMP.plt
grep -v "set te" $input_files | grep -v "set o" >> $TMP.plt
echo "quit" >> $TMP.plt
# If input file is specified AND command line contains plot commands, then
#   do command line args first, then plot commands in input file.
gnuplot $TMP.plt < /dev/null 
#gnuplot $TMP.plt $input_files

if ($status == 0 && -e $outfile && ! -z $outfile) then
    # The printer is whatever printer was last specified,
    # or the default printer if none was specified.
    # Imagen terminal driver support the page header
    if ($LANG == -Limpress) then
        /usr/local/bin/ipr $LANG $printer \
           -D"jobheader $print_banner" \
           -D"pagereversal on" \
           -D"program lasergnu" $outfile
    else if ($LANG == -Lpostscript) then
           lpr $lpr_opts $printer $outfile
    endif
else
    echo "lasergnu: error in plotting or empty plot; nothing printed."
endif

cleanup:
rm -f $TMP* $outfile
exit

usage:
echo "usage: lasergnu [-Pprinter] [-F font] [-w nx,ny ] [-s xscale,yscale][-b] [-O] [-p] [-t title] [-f file] ['plot command']...."
echo "  -O           : Orientation Portrait (default is Landscape)"
echo "  -F font      : fontsize in points for imagen"
echo "  -w nx,ny     : split page (nx and ny: no. plots horizontal and vertical)"
echo "  -s xscale,yscale : scaling in horiz, vertical direction"
echo "  -P printer   : Printer to send the plot"
echo "  -p           : set gnuplot driver to postscript (default is Imagen)"
echo "  -I           : set gnuplot terminal driver to imagen (default)"
echo "  -b           : don't print jobheader page"
echo ""
